package cn.silver.module.authority.domain;

import cn.silver.framework.core.utils.DateUtils;
import cn.silver.framework.core.utils.StringUtils;
import cn.silver.framework.mybatis.annotation.Table;
import cn.silver.framework.mybatis.annotation.TableField;
import cn.silver.framework.mybatis.enums.SearchEnums;
import cn.silver.framework.mybatis.domain.BusEntity;
import cn.silver.framework.core.enums.SystemStatusEnums;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.util.Date;

/**
 * 租户对象 sys_tenant
 *
 * @author Silver Zhou
 */
@Data
@NoArgsConstructor
@Table(name = "sys_tenant")
@EqualsAndHashCode(callSuper = true)
@Schema(name = "SysTenant", title = "系统租户信息")
public class SysTenant extends BusEntity {

    public final static String DEFALUT_CODE = "000000";
    @Serial
    private static final long serialVersionUID = 1L;
    /**
     * 租户编号
     */
    @Schema(title = "租户编号")
    @TableField(name = "code", searchType = SearchEnums.LIKE)
    private String code;
    /**
     * 租户名称
     */
    @Schema(title = "租户名称")
    @TableField(name = "name", searchType = SearchEnums.LIKE)
    private String name;
    /**
     * 域名
     */
    @Schema(title = "域名")
    private String domain;
    /**
     * 租户套餐编号
     */
    @Schema(title = "租户套餐编号")
    @TableField(name = "group_id", searchType = SearchEnums.EQ)
    private String groupId;
    /**
     * 注册时间
     */
    @Schema(title = "注册时间")
    @TableField(name = "start_time", searchType = SearchEnums.BETWEEN)
    @JsonFormat(timezone = DateUtils.TIME_ZONE, pattern = DateUtils.YYYY_MM_DD)
    private Date startTime;
    /**
     * 过期时间
     */
    @Schema(title = "过期时间")
    @JsonFormat(timezone = DateUtils.TIME_ZONE, pattern = DateUtils.YYYY_MM_DD)
    private Date stopTime;
    /**
     * 用户数量
     */
    @Schema(title = "用户数量")
    private Long capacity;
    /**
     * 联系电话
     */
    @Schema(title = "联系电话")
    private String phone;
    /**
     * 租户状态
     */
    @Schema(title = "租户状态")
    @TableField(name = "status", searchType = SearchEnums.EQ)
    private String status;

    public static SysTenant getDefault() {
        SysTenant tenant = new SysTenant();
        tenant.setCode(DEFALUT_CODE);
        tenant.setName("默认租户");
        tenant.setDomain("127.0.0.1");
        return tenant;
    }

    @Override
    public void preInsert() {
        super.preInsert();
        if (this.capacity == null) {
            this.capacity = -1L;
        }
        if (this.startTime == null) {
            this.startTime = DateUtils.getNowDate();
        }
        if (StringUtils.isBlank(this.status)) {
            this.status = SystemStatusEnums.NORMAL.getCode();
        }
    }
}
